Telegram Bot Server
This document provides comprehensive documentation for the Telegram Bot Server implementation. It explains the BotServer class architecture with dependency injection, the command handler system for /start, /help, /stop, /status, /stats, /noticestats, /userstats, and /web commands, user registration and management workflows, subscription handling, and admin command processing. It also documents the asynchronous bot lifecycle including initialization, polling mode operation, and graceful shutdown procedures. Additionally, it covers command handler implementations, user interaction patterns, message formatting with HTML and Markdown, integration with DatabaseService and NotificationService, error handling, logging mechanisms, daemon mode configuration, and security considerations for bot token management.
The Telegram Bot Server resides in the servers package and integrates with services and clients for database, notifications, and Telegram API communication. The main entry point orchestrates server startup and daemon mode.
app/servers/bot_server.py"] HANDLERS["Command Handlers
/start, /help, /stop, /status,
/stats, /noticestats, /userstats, /web"] end subgraph "Services" DB["DatabaseService
app/services/database_service.py"] NOTIF["NotificationService
app/services/notification_service.py"] TELEGRAM["TelegramService
app/services/telegram_service.py"] ADMIN["AdminTelegramService
app/services/admin_telegram_service.py"] STATS["PlacementStatsCalculatorService
app/services/placement_stats_calculator_service.py"] end subgraph "Clients" DBC["DBClient
app/clients/db_client.py"] TGCLI["TelegramClient
app/clients/telegram_client.py"] end subgraph "Core" CFG["Settings & Logging
app/core/config.py"] DMN["Daemon Utilities
app/core/daemon.py"] end MAIN --> BOT BOT --> HANDLERS BOT --> DB BOT --> NOTIF BOT --> ADMIN BOT --> STATS DB --> DBC NOTIF --> TELEGRAM TELEGRAM --> TGCLI BOT --> CFG MAIN --> DMN
Diagram sources
Section sources
BotServer: Implements the Telegram bot server with DI support, command handlers, lifecycle management, and admin command integration.
DatabaseService: Provides MongoDB operations for notices, jobs, placement offers, users, policies, and official placement data.
NotificationService: Aggregates multiple notification channels and orchestrates sending unsent notices.
TelegramService: Wraps Telegram Bot API interactions, message formatting, and broadcasting to users.
AdminTelegramService: Handles administrative commands with authentication and operational controls.
PlacementStatsCalculatorService: Computes comprehensive placement statistics from placement offers.
DBClient: Manages MongoDB connection and exposes collections.
TelegramClient: Low-level Telegram API client with retry logic and rate-limit handling.
Settings and Logging: Centralized configuration and logging setup with daemon mode support.
Section sources
The Telegram Bot Server follows a service-oriented architecture with dependency injection. The BotServer initializes services, registers command handlers, and manages the asynchronous lifecycle. Services encapsulate domain logic and integrate with clients for external APIs and databases.
Diagram sources
BotServer: Dependency Injection and Lifecycle#
Dependency Injection: BotServer accepts Settings, DatabaseService, NotificationService, AdminTelegramService, and PlacementStatsCalculatorService instances. It also supports daemon mode configuration.
Command Handlers Registration: setup_handlers registers handlers for /start, /help, /stop, /status, /stats, /noticestats, /userstats, and /web. Admin commands are conditionally registered if AdminTelegramService is provided.
Asynchronous Lifecycle:
run_async builds the Application, sets up logging, initializes and starts the application, and keeps the loop running while self.running is True.
run blocks and catches KeyboardInterrupt to trigger graceful shutdown.
shutdown stops the updater, application, and performs cleanup.
Factory Function: create_bot_server constructs DBClient, DatabaseService, TelegramService, NotificationService, AdminTelegramService, and PlacementStatsCalculatorService, then returns a BotServer instance.
Diagram sources
Section sources
Command Handler System#
/start: Registers or reactivates a user via DatabaseService and responds with HTML-formatted welcome message including commands and helpful links.
/help: Responds with Markdown-formatted help text listing available commands.
/stop: Deactivates a user via DatabaseService and informs the user.
/status: Retrieves user data from DatabaseService and formats a status message indicating subscription state.
/stats: Calculates placement statistics via PlacementStatsCalculatorService and replies with Markdown-formatted statistics.
/noticestats: Retrieves notice statistics from DatabaseService and replies with Markdown-formatted stats.
/userstats: Retrieves user statistics from DatabaseService and replies with Markdown-formatted stats (admin-only via AdminTelegramService).
/web: Replies with HTML-formatted links to JIIT suite tools.
Diagram sources
Section sources
User Registration and Management Workflows#
Registration: On /start, BotServer calls DatabaseService.add_user with user identity and chat metadata. Responses vary depending on whether the user is newly registered, reactivated, or already active.
Subscription Management: /stop triggers DatabaseService.deactivate_user to mark the user as inactive. /status retrieves user data and indicates active/inactive state.
User Statistics: AdminTelegramService provides /userstats to display total and active user counts via DatabaseService.get_users_stats.
Reactivate/Welcome text + commands + links"] Result --> |No| AlreadyActive{"Already active?"} AlreadyActive --> |Yes| AlreadyMsg["Build HTML message: already registered"] AlreadyActive --> |No| ErrorMsg["Build HTML message: error"] WelcomeMsg --> Send["reply_text with parse_mode=HTML"] AlreadyMsg --> Send ErrorMsg --> Send ReplyStartup --> Send Send --> End(["Done"])
Diagram sources
Section sources
Subscription Handling#
Deactivation: /stop calls DatabaseService.deactivate_user and informs the user accordingly.
Status Checking: /status fetches user data and formats a readable status message, including registration date and user ID when available.
Section sources
Admin Command Processing#
Authentication: AdminTelegramService verifies that the chat ID matches the configured admin chat ID.
/users: Lists all users with status and identifiers.
/boo: Broadcasts messages either to all users or a specific user.
/fu or /scrapyyy: Triggers a legacy update workflow executed in a thread to avoid blocking the event loop.
/kill: Stops the scheduler daemon.
/logs: Reads and returns the last lines of the specified log file, HTML-escaped and truncated to fit Telegram limits.
Diagram sources
Section sources
Asynchronous Bot Lifecycle#
Initialization: create_bot_server constructs dependencies and returns a BotServer instance.
Polling Mode: run_async initializes and starts the Application, then starts polling with drop_pending_updates to avoid replaying old updates.
Graceful Shutdown: run catches KeyboardInterrupt and calls shutdown to stop the updater and application cleanly.
Diagram sources
Section sources
Message Formatting with HTML and Markdown#
HTML: Used for /start welcome message, /web links, and status messages. TelegramService.convert_markdown_to_html transforms Markdown to HTML for Telegram.
Markdown: Used for /help and statistics commands. TelegramService.convert_markdown_to_telegram adapts Markdown for Telegram’s MarkdownV2.
Long Messages: TelegramService.split_long_message splits messages exceeding 4000 characters and sends them sequentially with delays.
Section sources
Integration with DatabaseService and NotificationService#
DatabaseService: Provides user management, notice operations, job operations, placement offers, and statistics retrieval. Used by BotServer for user registration/status and by AdminTelegramService for user listing and stats.
NotificationService: Orchestrates sending unsent notices to Telegram and Web Push channels. It fetches unsent notices from DatabaseService and broadcasts via TelegramService.
Section sources
Security Considerations for Bot Token Management#
Token Storage: BotServer reads TELEGRAM_BOT_TOKEN from Settings. TelegramClient validates presence of bot token before sending messages.
Admin Authentication: AdminTelegramService compares the incoming chat ID with the configured admin chat ID to restrict sensitive commands.
Daemon Mode: safe_print suppresses stdout in daemon mode and logs to files instead.
Section sources
The BotServer depends on DatabaseService, NotificationService, AdminTelegramService, and PlacementStatsCalculatorService. DatabaseService depends on DBClient. NotificationService depends on TelegramService. TelegramService depends on TelegramClient. The main entry point creates the BotServer and runs it, optionally in daemon mode.
Diagram sources
Section sources
Rate Limiting: TelegramService applies small delays between broadcasts to Telegram users to avoid rate limits.
Message Chunking: TelegramService.split_long_message ensures messages are within Telegram’s character limits.
Asynchronous Polling: BotServer uses asynchronous polling to keep the event loop responsive.
Daemon Mode: Reduces console output overhead in production environments.
[No sources needed since this section provides general guidance]
Bot not starting: Ensure TELEGRAM_BOT_TOKEN is configured; create_bot_server raises if token is missing.
Database connectivity: Verify MONGO_CONNECTION_STR; DBClient.connect tests the connection and logs errors.
Admin commands failing: Confirm admin chat ID matches the configured TELEGRAM_CHAT_ID; AdminTelegramService enforces authentication.
Logs: Use /logs admin command to retrieve recent log entries; logs are written to files configured by Settings.
Section sources
The Telegram Bot Server implements a robust, dependency-injected architecture with clear separation of concerns. It supports essential user commands, admin operations, and integrates with database and notification services. The asynchronous lifecycle, message formatting, and daemon mode configuration provide a production-ready foundation for Telegram-based notifications.
[No sources needed since this section summarizes without analyzing specific files]
Configuration and Environment Variables#
TELEGRAM_BOT_TOKEN: Telegram bot token.
TELEGRAM_CHAT_ID: Admin chat ID for authentication and default channel.
MONGO_CONNECTION_STR: MongoDB connection string.
SUPERSET_CREDENTIALS: JSON list of SuperSet credentials.
GOOGLE_API_KEY: Google API key for Gemini.
VAPID keys and contact email for web push.
WEBHOOK_PORT and WEBHOOK_HOST for webhook server.
LOG_LEVEL and LOG_FILE paths for logging.
DAEMON_MODE for suppressing stdout in background mode.
Section sources